Wiki
Clone wikilifev-release / tutorial / Read_a_ParameterList_datafile
Read a ParameterList datafile
We now focus on how to read an XML input datafile. To this end we focus on the xml file written in Write a ParameterList datafile. Let's suppose we have a main.cpp file wherein we aim at reading the content of the datafile (whose filename is xml_example.xml): to do that we need to
- include in the main file of the application the header files of the classes required.
- include in the main file of the application the lines of code which will actually read the input parameters.
The files to be included are those of the Trilinos package Teuchos. They are:
- Teuchos_ParameterList.hpp
- Teuchos_XMLParameterListHelpers.hpp
- Teuchos_RCP.hpp
First we need to create an RCP pointer to a ParameterList object; then, the xml input file is loaded by calling the function "getParametersFromXmlFile" giving as input the data filename. In the example reported, the instance of the ParameterList object (actually is an RCP pointer) is called XML_List. When reading a variable, one has to:
- specify the name of the list and sublists as input of the method "sublist"
- use the templated "get" method by specifying both the type (which has to match the one specified in the input xml file), its name and its default value (the latter used just in case in the datafile that variable is not present).
Below we report a mainXML.cpp file which reads an input ParameterList datafile. In the example shown below, after we loading the input parameters we ask process 0 (that is Process ID zero) to print out their values. Notice that in this way, in a parallel run of the code, only process 0 prints out the information.
#include <Epetra_ConfigDefs.h>
#ifdef EPETRAMPI
#include <Epetra_MpiComm.h>
#else
#include <Epetra_SerialComm.h>
#endif
#include <lifev/core/LifeV.hpp>
#include <Teuchos_ParameterList.hpp>
#include <Teuchos_XMLParameterListHelpers.hpp>
#include <Teuchos_RCP.hpp>
using namespace LifeV;
int main ( int argc, char** argv )
{
#ifdef HAVEMPI
MPI_Init ( &argc , &argv ) ;
std::shared_ptr< Epetra_Comm > Comm ( new Epetra_MpiComm ( MPI_COMM_WORLD ) ) ;
#else
std::shared_ptr< Epetra_Comm > Comm ( new Epetra_SerialComm ) ;
#endif
Teuchos::RCP< Teuchos::ParameterList > XML_List = Teuchos::rcp ( new Teuchos::ParameterList );
XML_List = Teuchos::getParametersFromXmlFile ( "xml_example.xml" );
std::string var1 = XML_List->sublist("section_AA").get<std::string>("parameter_one", "default");
int var2 = XML_List->sublist("section_AA").get<int>("parameter_two",0);
int var3 = XML_List->sublist("section_AA").sublist("subsectionA").get<int>("parameterA", 0);
bool var4 = XML_List->sublist("section_AA").sublist("subsectionB").get<bool>("parameterB", false);
double var5 = XML_List->sublist("section_BB").get<double>("another_parameter",0.1);
if ( Comm->MyPID() == 0 )
{
std::cout << std::endl << "section_AA/parameter_one = " << var1 << std::endl;
std::cout << "section_AA/parameter_two = " << var2 << std::endl;
std::cout << "section_AA/subsectionA/parameterA = " << var3 << std::endl;
std::cout << std::boolalpha << "section_AA/subsectionB/parameterB = " << var4 << std::endl;
std::cout << "section_BB/another_parameter = " << var5 << std::endl;
}
#ifdef HAVEMPI
MPI_Finalize () ;
#endif
return 0;
}
Here you can download the xml_example.xml and mainXML.cpp files.
Updated